import { createRouter, createWebHashHistory } from "vue-router";
import Home from "@/home/index.vue";
import Assembly from "@/view/Assembly/index.vue";
import Tutorial from "@/view/Tutorial/index.vue";
import Chat from "@/view/Chat/index.vue";
import Login from "@/view/Login/index.vue";
import Temp from "@/view/Temp/index.vue";
import Info from "@/view/Info/index.vue";
import AssInfo from "@/view/AssInfo/index.vue";
import User from "@/view/User/index.vue";
import AI from "@/view/hdcAI/index.vue";


const routes = [
  {
    path: "/",
    redirect: "/home",
    component: Temp,
    children: [
      {
        path: "/home",
        name: "Home",
        component: Home,
        meta: {},
      },
      {
        path: "/assembly",
        name: "Assembly",
        component: Assembly,
        meta: {},
      },
      {
        path: "/tutorial",
        name: "Tutorial",
        component: Tutorial,
        meta: {},
      },
      {
        path: "/chat",
        name: "Chat",
        component: Chat,
        meta: {},
      },
    ],
  },

  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {},
  },
  {
    //携带id
    path: `/info`,
    name: "Info",
    component: Info,
    meta: {},
  },
  {
    //携带id
    path: `/assinfo`,
    name: "AssInfo",
    component: AssInfo,
    meta: {},
  },
  {
    path: "/user",
    name: "User",
    component: User,
    meta: {},
  },
  {
    path: "/hdcAI",
    name: "AI",
    component: AI,
    meta: {},
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

//路由拦截
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem("token");
  if (to.path === "/login" && isLoggedIn !== null) {
    // 如果用户已经登录且尝试访问登录页面，则重定向到主页

    router.back();
  } else {
    next();
  }
});
export default router;
